home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / titlemgt / quitquer.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  6.3 KB  |  249 lines  |  [TEXT/ttxt]

  1. --<<<-
  2.  
  3. module QuitQueryExample
  4.     uses ScriptX
  5.     exports SafeTitleContainer, InterfaceButton
  6. end
  7. in module QuitQueryExample
  8.  
  9. class InterfaceButton (PushButton)
  10.     class variables
  11.         buttonBoundary:(new Rect x2:60 y2:20)
  12.     instance variables
  13.         text
  14.     instance methods
  15.         method init self #rest args #key \
  16.                 buttonText: -> (
  17.             if isAKindOf buttonText String then (
  18.                 self.text := buttonText
  19.             ) else (
  20.                 report badParameter #(buttonText, init,
  21.                     self, "not a string")
  22.             )
  23.             apply nextMethod self \
  24.                 releasedPresenter:(new TextPresenter \
  25.                     boundary:InterfaceButton.buttonBoundary \                                                         
  26.                     target:(self.text) \
  27.                     stroke:blackBrush) \
  28.                 pressedPresenter:(new TwoDShape \
  29.                     target:InterfaceButton.buttonBoundary \
  30.                     fill:blackBrush) \
  31.                 args
  32.         )
  33.         method afterInit self #rest args #key \
  34.                 action: -> (
  35.             setDefaultAttr self.releasedPresenter @alignment @center
  36.             if isAKindOf action AbstractFunction then (
  37.                 self.activateAction := action
  38.             ) else (
  39.                 report badParameter #(action, afterInit,
  40.                     self, "not a function")
  41.             )
  42.         )
  43. end
  44.  
  45. class YesNoWindow (Window)
  46.     instance variables
  47.         actuatorController
  48.         textQuery
  49.         textQueryPresenter
  50.         yesButton
  51.         noButton
  52.         theResult:false
  53.         theLock:(new Lock)
  54.         startPipe:(new PipeClass size:1)
  55.         finishPipe:(new PipeClass size:1)
  56. end
  57.  
  58. method init self {class YesNoWindow} #rest args -> (
  59.     apply nextMethod self type:@notice args
  60. )
  61.  
  62. method afterInit self {class YesNoWindow} #rest args \
  63.         #key question: xPosition: yPosition: -> (
  64.     apply nextMethod self args
  65.     self.actuatorController := new ActuatorController \
  66.             space:self \
  67.             wholeSpace:true
  68.     if isAKindOf question String then
  69.         self.textQuery := question
  70.     else
  71.         report badParameter #(question, afterInit,
  72.             self, "not a string")
  73.     self.textQueryPresenter := new TextPresenter \
  74.             boundary:(new Rect x2:320 y2:20) \
  75.             target:(self.textQuery)
  76.     self.yesButton := new InterfaceButton \
  77.             buttonText:"Yes" \
  78.             action:(a b -> (
  79.                 if a.title != theScratchTitle then (
  80.                     a.theResult := true
  81.                 ) else (
  82.                     a.theResult := false
  83.                 )); hide a; 
  84.                 write a.finishPipe a.theResult)
  85.     self.yesButton.authorData := self
  86.     self.noButton := new InterfaceButton \
  87.             buttonText:"No" \
  88.             action:(a b -> 
  89.                 a.theResult := false
  90.                 hide a; 
  91.                 write a.finishPipe a.theResult)
  92.     self.noButton.authorData := self
  93.         
  94.     -- center the text, so it looks good
  95.     setDefaultAttr self.textQueryPresenter @alignment @center
  96.  
  97.     -- position the three presenters, based on window's dimensions    
  98.     local buttonHeight := (self.height * 0.75) as ImmediateInteger
  99.     local buttonCenter := (self.width * 0.50) as ImmediateInteger
  100.     self.yesButton.y := buttonHeight
  101.     self.noButton.y := buttonHeight
  102.     self.yesButton.x := buttonCenter + 10
  103.     self.noButton.x := (buttonCenter - self.noButton.width) - 10
  104.     self.textQueryPresenter.x := buttonCenter - (self.textQueryPresenter.width / 2)
  105.     self.textQueryPresenter.y := (self.height * 0.30) as ImmediateInteger
  106.     
  107.     -- position self
  108.     self.x := xPosition
  109.     self.y := yPosition
  110.  
  111.     -- add the three presenters
  112.     append self self.yesButton
  113.     append self self.noButton
  114.     append self self.textQueryPresenter
  115. )
  116.  
  117. method askForYesOrNo self {class YesNoWindow} -> (
  118.     acquire self.theLock
  119.     write self.startPipe true -- unblocks calling thread
  120.     show self
  121.     local myResult := read self.finishPipe -- block 
  122.     relinquish self.theLock -- unblocks calling thread
  123.     return myResult
  124. )
  125.  
  126. class SafeTitleContainer (TitleContainer)
  127.     instance variables
  128.         transient _quitQueryID
  129.     instance methods
  130.         method quitQueryIDGetter self ->
  131.             self._quitQueryID
  132.         method quitQueryIDSetter self value ->
  133.             if getClass value == ImmediateInteger then
  134.                 self._quitQueryID := value
  135.             else
  136.                 report badParameter #(value, quitQueryIDSetter,
  137.                     self, "not an immediate integer")
  138.         method okayToCloseTitle self -> (
  139.             bringToFront self
  140.             local buildText := "Is it OK to quit the title " +
  141.                 self.name + "?"
  142.             local a := new YesNoWindow \
  143.                     boundary:(new Rect x2:400 y2:100) \
  144.                     question:buildText \
  145.                     title:self \
  146.                     xPosition:50 \
  147.                     yPosition:50 
  148.             local modalProcess := new Thread \
  149.                     func:askForYesOrNo \
  150.                     arg:a
  151.             read a.startPipe
  152.             acquire a.theLock
  153.             print "I was here five"
  154.             local b := modalProcess.result
  155.             return b
  156.         )
  157.         method close self #rest args -> (
  158.             if okayToCloseTitle self then (
  159.                 deinstallQuitQuery self.quitQueryID
  160.                 apply nextMethod self args
  161.             ) 
  162.         )
  163.         method startupActionGetter self -> (
  164.             -- make sure only a single quit query is installed
  165.             if self.quitQueryID = undefined do
  166.                 self.quitQueryID := 
  167.                     installQuitQuery okayToCloseTitle self
  168.             nextMethod self
  169.         )
  170. end
  171.  
  172. module QuitQueryTest1 
  173.     uses ScriptX
  174.     uses QuitQueryExample
  175. end
  176. in module QuitQueryTest1
  177.  
  178. object mySafeTitle (SafeTitleContainer)
  179.         dir:theStartDir
  180.         path:"quitqone.sxt"
  181.         name:"Wade's Follies"
  182. end
  183.  
  184. open TitleContainer path:"quitqone.sxt"
  185. object myWindow (Window)
  186.         title:mySafeTitle
  187.         name:mySafeTitle.name
  188. end
  189. object buttonController (ActuatorController)
  190.     space:myWindow, wholeSpace:true, enabled:true
  191. end
  192. object myCloseButton (InterfaceButton)
  193.         buttonText:"Close"
  194.         action:(a b -> new Thread \
  195.             func:(c -> close c) arg:a)
  196.     settings
  197.         x:420, y:360
  198. end
  199. myCloseButton.authorData := myWindow.title
  200. object myQuitButton (InterfaceButton)
  201.         buttonText:"Quit"
  202.         action:(a b -> new Thread func:(c -> quit()))
  203.     settings
  204.         x:500, y:360
  205. end
  206. myQuitButton.authorData := myWindow.title
  207. append myWindow myCloseButton
  208. append myWindow myQuitButton
  209. add mySafeTitle 1 myWindow
  210. show myWindow
  211.  
  212. module QuitQueryTest2 uses ScriptX, QuitQueryExample end
  213. in module QuitQueryTest2
  214.  
  215. object mySafeTitle (SafeTitleContainer)
  216.         dir:theStartDir
  217.         path:"quitqtwo.sxt"
  218.         name:"Ross's Peccadillos"
  219. end
  220. open TitleContainer path:"quitqtwo.sxt"
  221. object myWindow (Window)
  222.         title:mySafeTitle
  223.         name:mySafeTitle.name
  224. end
  225. object buttonController (ActuatorController)
  226.     space:myWindow, wholeSpace:true, enabled:true
  227. end
  228. object myCloseButton (InterfaceButton)
  229.         buttonText:"Close"
  230.         action:(a b -> new Thread \
  231.             func:(c -> close c) arg:a)
  232.     settings
  233.         x:420, y:360
  234. end
  235. myCloseButton.authorData := myWindow.title
  236. object myQuitButton (InterfaceButton)
  237.         buttonText:"Quit"
  238.         action:(a b -> new Thread func:(c -> quit()))
  239.     settings
  240.         x:500, y:360
  241. end
  242. myQuitButton.authorData := myWindow.title
  243. append myWindow myCloseButton
  244. append myWindow myQuitButton
  245. add mySafeTitle 1 myWindow
  246. show myWindow
  247.  
  248. -->>>
  249.